import { ref } from 'vue';
import dayjs from 'dayjs';
import { useAuthStore } from '@/store/modules/auth';
import CheckFQCAdd from '../../modal/check-FQC-add.vue';
import CheckFQCViewStandard from '../../modal/check-FQC-view-standard.vue';

function getTime() {
  const now = new Date();
  now.setHours(8, 0, 0, 0);
  const date = [
    dayjs(now - 1000 * 60 * 60 * 24).format('YYYY-MM-DD HH:mm:ss'),
    dayjs(now).format('YYYY-MM-DD HH:mm:ss')
  ];
  return date;
}

const visible = ref(false);
const currentRow = ref(null);
const modalOperateType = ref('add');

const modalVisible = ref(false);

const authStore = useAuthStore();
export default {
  url: '/qms/fqc/check/list',
  addUrl: '/qms/fqc/check/add',
  editUrl: '/qms/fqc/check/update',
  delUrl: '/qms/fqc/check/deleteBatch',
  editMethod: 'put',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'select',
      key: 'region',
      label: '区域',
      placeholder: '请选择区域',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6,
      selectOptions: [
        {
          value: '1',
          label: '一期'
        },
        {
          value: '2',
          label: '二期'
        }
      ]
    },
    {
      type: 'input',
      key: 'operator',
      label: '检验员',
      placeholder: '请输入检验员',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },
    {
      type: 'select',
      key: 'shiftType',
      label: '班别',
      placeholder: '请选择班别',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 6,
      selectOptions: [
        {
          value: '0',
          label: '白班'
        },
        {
          value: '1',
          label: '夜班'
        }
      ]
    },

    {
      type: 'dateRangePicker',
      key: 'time',
      label: '日期',
      showTime: true,
      defaultValue: getTime(),
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  searchParam: ref({
    moduleNo: null,
    operator: null,
    state: null,
    region: null,
    shiftType: '0',
    time: getTime()
  }),
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'shiftType',
      dataIndex: 'shiftType',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['白班', '夜班'][record.shiftType];
      }
    },
    {
      key: 'modifyTime',
      dataIndex: 'modifyTime',
      title: '时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'fqcCheckType',
      dataIndex: 'fqcCheckType',
      title: '检查类型',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'dateCode',
      dataIndex: 'dateCode',
      title: '周期',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'totalCheckNum',
      dataIndex: 'totalCheckNum',
      title: '总检查数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'okNum',
      dataIndex: 'okNum',
      title: 'OK数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'ngNum',
      dataIndex: 'ngNum',
      title: '不良数',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'ngRate',
      dataIndex: 'ngRate',
      title: '不良率',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'machineNo',
      dataIndex: 'machineNo',
      title: '机台号',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'region',
      dataIndex: 'region',
      title: '区域',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        // 1是一期，2是二期
        return ['一期', '二期'][record.region - 1];
      }
    },

    {
      key: 'areaTrea',
      dataIndex: 'areaTrea',
      title: '表面工艺',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'fqcArea',
      dataIndex: 'fqcArea',
      title: '面积(ft2)',
      width: 150,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '检验员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],
  isCustomAdd: true,
  rednderComponents: [
    () => {
      return (
        <CheckFQCAdd
          visible={visible.value}
          rowData={currentRow.value}
          operateType={modalOperateType.value}
          onAddModalClose={() => (visible.value = false)}
          onAddSuccess={() => {}}
        ></CheckFQCAdd>
      );
    },
    () => (
      <CheckFQCViewStandard
        visible={modalVisible.value}
        onClose={() => (modalVisible.value = false)}
      ></CheckFQCViewStandard>
    )
  ],
  openModal(val) {
    visible.value = true;
    currentRow.value = val.currentRow;
    modalOperateType.value = val.operateType;
  },
  otherBtnList: [
    {
      icon: 'icon-chakanbiaozhun',
      name: '查看标准',
      iconType: 3,
      auth: 'production-management:86:lookStandard',
      handleClick() {
        modalVisible.value = true;
      }
    }
  ],
  scroll: { x: 2500, y: 'auto' },
  modalClose() {
    moduleNoSearchData.value = null;
  }
};
